<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column align="center" label="序号" type="index" :index="indexMethod" />${range .TableFields}
      <el-table-column align="center" label="${.ColumnComment}">
        <template slot-scope="scope">
          <span :title="scope.row.${.ColumnName | formatSmallCamel}">{{ scope.row.${.ColumnName | formatSmallCamel} }}</span>
        </template>
      </el-table-column>${end}
      <el-table-column fixed="right" align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="${.TableName}" :visible.sync="dialogVisible">
      <el-form ref="${.TableName | formatSmallCamel}Form" :model="${.TableName | formatSmallCamel}" :rules="rules" size="mini">${range .FormFields}
        <el-form-item label="${.ColumnComment}" prop="${.ColumnName | formatSmallCamel}" :label-width="formLabelWidth">
          <el-input v-model="${$.TableName | formatSmallCamel}.${.ColumnName | formatSmallCamel}" autocomplete="off" />
        </el-form-item>${end}
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { del, get, getPage, save, update } from '@/api/${.TableName | formatBigCamel}'

export default {
  data() {
    return {
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        size: 10
      },
      ${.TableName | formatSmallCamel}: {
      ${range $index, $field := .FormFields}${if lt $index $.FormFieldLength}  ${$field.ColumnName | formatSmallCamel}: null,${else}  ${$field.ColumnName | formatSmallCamel}: null${end}
      ${end}},
      ${.TableName | formatSmallCamel}Copy: null,
      rules: {
      ${range $index, $field := .RuleFields}${if lt $index $.RuleFieldLength}  ${$field.ColumnName | formatSmallCamel}: [{ required: true, message: '请填写${$field.ColumnComment}', trigger: 'blur' }],${else}  ${$field.ColumnName | formatSmallCamel}: [{ required: true, message: '请填写${$field.ColumnComment}', trigger: 'blur' }]${end}
      ${end}},
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage() {
      this.listLoading = true
      getPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getPage()
    },
    indexMethod(index) {
      return this.listQuery.size * (this.listQuery.page - 1) + index + 1
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },
    handleCreate() {
      Object.keys(this.${.TableName | formatSmallCamel}).forEach(key => (this.${.TableName | formatSmallCamel}[key] = null))
      this.${.TableName | formatSmallCamel}Copy = null
      this.dialogVisible = true
      if (this.$refs['${.TableName | formatSmallCamel}Form'] !== undefined) {
        this.$refs['${.TableName | formatSmallCamel}Form'].clearValidate()
      }
    },
    handleEdit(row) {
      const params = { id: row.${.FieldIdName | formatSmallCamel} }
      get(params).then(response => {
        this.${.TableName | formatSmallCamel} = response.data
        this.${.TableName | formatSmallCamel}Copy = response.data
      })
      this.dialogVisible = true
      if (this.$refs['${.TableName | formatSmallCamel}Form'] !== undefined) {
        this.$refs['${.TableName | formatSmallCamel}Form'].clearValidate()
      }
    },
    handleDelete(row) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.${.FieldIdName | formatSmallCamel}).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    handleSaveOrUpdate() {
      this.$refs['${.TableName | formatSmallCamel}Form'].validate((valid) => {
        if (valid) {
          if (this.${.TableName | formatSmallCamel}.${.FieldIdName | formatSmallCamel}) {
            update(this.${.TableName | formatSmallCamel}).then(() => {
              this.$message({
                type: 'success',
                message: '更新成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          } else {
            save(this.${.TableName | formatSmallCamel}).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          }
        } else {
          this.$message.error('请保证数据填写正确！')
        }
      })
    }
  }
}
</script>

<style>
</style>
